import React, { useEffect, useState } from 'react'
import { Input, Tree } from 'antd'
import '../../../assets/css/RolePermission/newrole.css'
import Service from '../../../service'
import { inject, observer } from 'mobx-react'


function NewRole(props) {
    const [treeData, setTreeData] = useState([])
    useEffect(() => {
        getList()
    }, [])
    const getList = async () => {
        let result = await Service.StaffManage.RolePermission.getListMenu()
        const data = JSON.parse(JSON.stringify(result.data))
        console.log(data);
        function arrTotree(arr) {
            const hash = {};
            const result = [];
            for (let item of arr) {
                hash[item.id] = item;
                item.key = item.id
                item.icon = ''
            }
            for (let item of arr) {
                const parent = hash[item.pid];
                if (parent) {
                    if (!parent.children) {
                        parent.children = [];
                    }
                    parent.children.push(item);
                } else {
                    result.push(item);
                }
            }
            return result;
        }


        setTreeData(arrTotree(data))
    }
    const onChange = (e) => {
        props.departmentSettingsStore.setTitle(e.target.value)
    }
    const onCheck = (checkedKeys, info) => {
        props.departmentSettingsStore.setNewMenu_id(checkedKeys)
        console.log('onCheck', checkedKeys, info);
    };
    return (
        <div id='NewRole'>
            <div className='name'>
                <p>*新增角色名称</p>  <Input onChange={(e) => onChange(e)} type="text" value={props.departmentSettingsStore.newTitle} />
            </div>
            <div>
                <Tree
                    showIcon
                    checkable
                    defaultSelectedAll
                    onCheck={onCheck}
                    treeData={treeData}
                />
            </div>
        </div>
    )
}

export default inject('departmentSettingsStore')(observer(NewRole))
